package com.tyler.ack;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

/**
 * @author: TylerZhong
 * @description:
 */
public class AsyncSendProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("asyncSendProducerGroup");
        producer.setNamesrvAddr("192.168.56.191:9876");
        producer.start();

        Message message = new Message("Order","tag","order info : orderId = xxx".getBytes(StandardCharsets.UTF_8));

        CountDownLatch countDownLatch = new CountDownLatch(1);
        // 异步发送
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                countDownLatch.countDown();
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable e) {
                countDownLatch.countDown();
                e.printStackTrace();
            }
        });

        countDownLatch.await();

        Thread.sleep(5000);
        producer.shutdown();
    }
}
